import 'package:flutter/material.dart';
import 'package:flutter_app/config/config.dart';

final staticImageUrl = RegExp('^images');

class CommonImage extends StatefulWidget {
  const CommonImage(
      {Key? key,
      required this.src,
      this.width = 50,
      this.height = 50,
      this.defImagePath = 'images/default.jpeg',
      this.fit = BoxFit.cover})
      : super(key: key);
  final String src;
  final double width;
  final double height;
  final String defImagePath;
  final BoxFit fit;
  @override
  _CommonImageState createState() => _CommonImageState();
}

class _CommonImageState extends State<CommonImage> {
  late Image _image;
  @override
  void initState() {
    if (staticImageUrl.hasMatch(widget.src)) {
      _image = Image.asset(
        widget.src,
        width: widget.width,
        height: widget.height,
        fit: widget.fit,
      );
    } else {
      _image = Image.network(
        Config.baseUrl + widget.src,
        width: widget.width,
        height: widget.height,
        fit: widget.fit,
      );
    }
    super.initState();

    var resolve = _image.image.resolve(ImageConfiguration.empty);
    resolve.addListener(ImageStreamListener((_, __) {
      //加载成功
    }, onError: (Object exception, StackTrace? stackTrace) {
      //加载失败
      setState(() {
        _image = Image.asset(
          widget.defImagePath,
          width: widget.width,
          height: widget.height,
        );
      });
    }));
  }

  @override
  Widget build(BuildContext context) {
    return _image;
  }
}

// class CommonImage extends StatelessWidget {
//   const CommonImage(
//       {Key? key,
//       required this.src,
//       this.width = 50,
//       this.height = 50,
//       this.defImagePath = '/images/default.jpeg',
//       this.fit = BoxFit.cover})
//       : super(key: key);
//   final String src;
//   final double width;
//   final double height;
//   final String defImagePath;
//   final BoxFit fit;



//   @override
//   Widget build(BuildContext context) {
//     if (staticImageUrl.hasMatch(src)) {
//       return Image.asset(
//         src,
//         width: width,
//         height: height,
//         fit: fit,
//       );
//     } else {
//       return Image.network(
//         src,
//         width: width,
//         height: height,
//         fit: fit,
//       );
//     }
//     return Container();
//   }
// }
// final networkImageUrl = RegExp('^http');

// class ImageCommom extends StatelessWidget {
//   final String src;
//   final double width;
//   final double height;
//   final BoxFit fit;
//   const ImageCommom(this.src,
//       {Key? key, this.width = 0, this.height = 0, this.fit = BoxFit.cover})
//       : super(key: key);

//   @override
//   Widget build(BuildContext context) {
//     if (networkImageUrl.hasMatch(src)) {
//       return Image(
//           width: width,
//           height: height,
//           fit: fit,
//           image: AdvancedNetworkImage(src,
//               useDiskCache: true,
//               cacheRule: const CacheRule(maxAge: Duration(days: 7)),
//               timeoutDuration: const Duration(seconds: 25)));
//     }
//     if (staticImageUrl.hasMatch(src)) {
//       return Image.asset(
//         src,
//         width: width,
//         height: height,
//         fit: fit,
//       );
//     }
//     assert(false, '图片地址不合法');
//     return Container();
//   }
// }
